package com.app.car.bean;


import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import com.app.car.greendao.GreenDaoUtils;
import com.google.gson.Gson;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;

import java.util.ArrayList;
import java.util.List;

import my.TimeUtils;
import org.greenrobot.greendao.annotation.Generated;

@Entity(indexes = {@Index(value = "time DESC", unique = true)})
public class RfidListBean extends BasisBean {



    @Id(autoincrement = true)
    private Long id;

    private static final long serialVersionUID = 1L;
    @Convert(columnType = String.class, converter = RfidBeanConverter.class)
    public ArrayList<RfidListData> checkRfidList = new ArrayList<>();    //盘点到的数据


    @Convert(columnType = String.class, converter = RfidBeanConverter.class)
    public ArrayList<RfidListData> panDaoList = new ArrayList<RfidListData>();    //盘到的数据       仓库里面有的并且被盘点机扫描出来的

    @Convert(columnType = String.class, converter = RfidBeanConverter.class)
    public ArrayList<RfidListData> panYingList = new ArrayList<RfidListData>();    //盘盈的数据

    @Convert(columnType = String.class, converter = RfidBeanConverter.class)
    public ArrayList<RfidListData> panKuiList = new ArrayList<RfidListData>();    //盘亏的数据


    public long time = 0;   //时间戳

    public String linkWare;   //用来连接数据库loc字段



    @Generated(hash = 745019891)
    public RfidListBean(Long id, ArrayList<RfidListData> checkRfidList, ArrayList<RfidListData> panDaoList,
            ArrayList<RfidListData> panYingList, ArrayList<RfidListData> panKuiList, long time, String linkWare) {
        this.id = id;
        this.checkRfidList = checkRfidList;
        this.panDaoList = panDaoList;
        this.panYingList = panYingList;
        this.panKuiList = panKuiList;
        this.time = time;
        this.linkWare = linkWare;
    }

    @Generated(hash = 740227926)
    public RfidListBean() {
    }



    public String getLinkWare() {
        if(TextUtils.isEmpty(linkWare))return "";
        return linkWare;
    }

    public void setLinkWare(String linkWare) {
        this.linkWare = linkWare;
    }


    public ArrayList<RfidListData> getCheckRfidList() {
        return checkRfidList;
    }

    public void setCheckRfidList(ArrayList<RfidListData> checkRfidList) {
        this.checkRfidList = checkRfidList;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        if(this.time != 0){
            return;
        }
        this.time = time;
    }

    @JSONField(serialize = false)
    public boolean isContains(ArrayList<RfidListData> rfidListData,String rfid){
        if(TextUtils.isEmpty(rfid))return true;
        for(RfidListData data:rfidListData){
            if(data.getRfid().equals(rfid)){
                return true;
            }
        }
        return false;
    }

    @JSONField(serialize = false)
    public void addData(String rfid,int ant){
        RfidListData data = GreenDaoUtils.getRfidListDataByRfid(rfid);
        data.setTime(System.currentTimeMillis());
        data.setAnt(ant);
        data.setYear(TimeUtils.getTimeLongToStrByFormat(data.getTime(), "yyyy"));
        data.setMonth(TimeUtils.getTimeLongToStrByFormat(data.getTime(), "MM"));
        checkRfidList.add(data);
    }

    @JSONField(serialize = false)
    public void addData(RfidListData data,int ant){
        data.setTime(System.currentTimeMillis());
        data.setAnt(ant);
        data.setYear(TimeUtils.getTimeLongToStrByFormat(data.getTime(), "yyyy"));
        data.setMonth(TimeUtils.getTimeLongToStrByFormat(data.getTime(), "MM"));
        checkRfidList.add(data);
    }

    @JSONField(serialize = false)
    public void addBean(ArrayList<RfidListData> rfidListData,String rfid){
        if(isContains(rfidListData,rfid))return;
        RfidListData data = new RfidListData();
        data.setRfid(rfid);
        data.setTime(System.currentTimeMillis());
        data.setYear(TimeUtils.getTimeLongToStrByFormat(data.getTime(), "yyyy"));
        data.setMonth(TimeUtils.getTimeLongToStrByFormat(data.getTime(), "MM"));
        rfidListData.add(data);
    }

    @JSONField(serialize = false)
    public void toCheck(){
         List<RfidListData> bookRfidList = getBookRfidList();
        if(bookRfidList == null || bookRfidList.size()<1){
            for (RfidListData data : checkRfidList){
                addBean(panYingList,data.getRfid());
            }
        }else {
            for(RfidListData checkData : checkRfidList){
                boolean isEquals = false;
                for (RfidListData bookData : bookRfidList){
                    if(bookData.getRfid().equals(checkData.getRfid())){
                        isEquals = true;
                        addBean(panDaoList,checkData.getRfid());
                        break;
                    }
                }
                if(!isEquals){
                    addBean(panYingList,checkData.getRfid());
                }
            }
            //计算盘亏
            ArrayList<RfidListData> kuiList = new ArrayList<>();
            for(RfidListData bookData : bookRfidList){
                boolean isEquals = false;
                for (RfidListData checkData : checkRfidList){
                    if(checkData.getRfid().equals(bookData.getRfid())){
                        isEquals = true;
                        break;
                    }
                }
                if(!isEquals){
                    kuiList.add(bookData);
                }
            }
            panKuiList = kuiList;
        }
    }



    public ArrayList<RfidListData> getBookRfidList() {
//        if(TextUtils.isEmpty(getLinkWare())){
            ArrayList<RfidListData> bookRfidList = (ArrayList<RfidListData>) GreenDaoUtils.getRfidListData();
            return bookRfidList;
//        }else {
//            ArrayList<RfidListData> bookRfidList = (ArrayList<RfidListData>) GreenDaoUtils.getRfidListDataByWare(getLinkWare());
//            return bookRfidList;
//        }
    }



    public ArrayList<RfidListData> getPanYingList() {
        return this.panYingList;
    }

    public void setPanYingList(ArrayList<RfidListData> panYingList) {
        this.panYingList = panYingList;
    }

    public ArrayList<RfidListData> getPanKuiList() {
        return this.panKuiList;
    }

    public ArrayList<RfidListData> getPanDaoList() {
        return panDaoList;
    }

    public void setPanDaoList(ArrayList<RfidListData> panDaoList) {
        this.panDaoList = panDaoList;
    }


    public void setPanKuiList(ArrayList<RfidListData> panKuiList) {
        this.panKuiList = panKuiList;
    }


    @JSONField(serialize = false)
    public ArrayList<RfidListData> StringToArray(String databaseValue) {
        if (databaseValue == null) {
            return new ArrayList<>();
        }
        ArrayList<RfidListData> rfidList = JSON.parseObject(databaseValue,new TypeReference<ArrayList<RfidListData>>(){}); //
        return rfidList;
    }

    @JSONField(serialize = false)
    public String arrayToString(ArrayList<RfidListData> arrays) {
        if (arrays == null) {
            return "";
        } else {
            return new Gson().toJson(arrays);
        }
    }


    @JSONField(serialize = false)
    public int getMaxSize(){
      return Math.max(getBookRfidList().size(),checkRfidList.size());
    }

}
